﻿using System;
using System.Collections.Generic;
using System.Text;
using BusinessLogic.BusinessRules;
using BusinessLogic.ClassFactory;
using RichPlugins;

namespace IssueCommon
{
    public class ReplaceAPlugin
    {
        public static string ReplaceSectionSubPlugin(Plugin objPlugin, int _intDefaultASID, SessionUser objSessionUser)
        {
            string strXml = string.Empty;

            string strArtSecID = objPlugin.GetValueByTagName("id");	// 文章ID
            int intArtSecID = -1;
            if (strArtSecID == string.Empty)	// 不是指定文章ID,采用当前ID
            {
                intArtSecID = _intDefaultASID;
            }
            else
            {
                intArtSecID = int.Parse(strArtSecID);
            }

            using (SectionsFactory objFactory = new SectionsFactory())
            {
                Sections objSectins = objFactory.CreatorByArticleID(intArtSecID);
                if (objSectins != null)
                {
                    objSectins.SessionUser = objSessionUser;
                    strXml = objSectins.GetOneSectionXml();
                }
                else
                {
                    string strMsg = string.Format("未找到文ID={0}的栏目", intArtSecID);
                    throw new Model.Common.CustomException(strMsg);
                }
            }
            return strXml;
        }

        /// <summary>
        /// 处理文章字段插件
        /// </summary>
        /// <param name="objPlugin"></param>
        /// <returns></returns>
        public static string ReplaceArticleFieldsPlugin(Plugin objPlugin, int _intDefaultASID, SessionUser objSessionUser)
        {
            int _intDefaultParentID = -1;
            string strXml = string.Empty;
            string strArtSecID = objPlugin.GetValueByTagName("id");	// 文章ID
            string strSectionID = objPlugin.GetValueByTagName("stid");	// 文章所在栏目ID
            bool bolDisabled = false;
            //是否禁用
            string strDisabled = objPlugin.GetValueByTagName("disabled");
            if (strDisabled != string.Empty)
            {
                if (strDisabled == "1")
                {
                    bolDisabled = true;
                }
            }

            if (bolDisabled == false)
            {
                int intArtSecID = -1;
                if (strArtSecID == string.Empty)	// 不是指定文章ID,采用当前ID
                {
                    intArtSecID = _intDefaultASID;
                }
                else
                {
                    intArtSecID = int.Parse(strArtSecID);
                }

                if (strSectionID != string.Empty)
                {
                    _intDefaultParentID = int.Parse(strSectionID);
                }

                //strXml = GetCachXml(intArticleID, Model.Common.Plugin.PluginType.ArticleFields, 0);
                //// 有缓存的Xml,返回缓存数据
                //if (strXml != string.Empty)
                //{
                //    return strXml;
                //}

                using (ArticleFactory objFactory = new ArticleFactory())
                {
                    ArtSecRelationFactory objacFactory = new ArtSecRelationFactory();
                    ArtSecRelation objArtSec = objacFactory.Creator(intArtSecID);
                    Article objArticle = objFactory.NewInstance();
                    if (objArtSec == null)
                    {
                        string strMsg = string.Format("未找到文章，关联ID={0}，原因可能是在站点、栏目的模板中放置了文章插件，请查看是否有type=4的插件", intArtSecID);
                        throw new IssueCommon.CustomException(strMsg);
                    }
                    else
                    {
                        if (_intDefaultParentID == -1)
                        {
                            objArticle = objFactory.CreatorBySql(" intArticleId=" + objArtSec.DataItem.intArticleID + " ");
                            if (objArtSec == null)
                            {
                                string strMsg = string.Format("未找到文章ID (ID={0})，原因可能是该篇文章已经不存在",
                                                              objArtSec.DataItem.intArticleID);
                                throw new IssueCommon.CustomException(strMsg);
                            }
                            else
                            {
                                if (objArticle != null)
                                {
                                    _intDefaultParentID = objArtSec.DataItem.intSectionID;
                                    strXml = objArticle.GetArticleHavecontentXml(_intDefaultParentID, intArtSecID, objSessionUser);
                                }
                            }
                        }
                        else
                        {
                            strXml = objArticle.GetArticleHavecontentXml(_intDefaultParentID, intArtSecID, objSessionUser);
                        }
                    }
                    objacFactory.Dispose();
                }

                // 将Xml缓存
                //XmlCach objXmlCach = new XmlCach(intArticleID, Model.Common.Plugin.PluginType.ArticleFields, 0, strXml);
                //m_aryXmlCach.Add(objXmlCach);
            }
            else
            {
                strXml = Common.Xml.XmlHelper.GetNewXMLDoc();
            }

            return strXml;
        }

        /// <summary>
        /// 当前文章所在栏目的文章列表
        /// </summary>
        /// <param name="objPlugin"></param>
        /// <returns></returns>
        public static string ReplaceParentSectionArticleListPlugin(Plugin objPlugin, int _intDefaultASID, SessionUser sessionUser)
        {
            int _intDefaultParentID = -1;
            string strXml = string.Empty;
            string strArtSecID = objPlugin.GetValueByTagName("id");	// 文章ID
            string strSectionID = objPlugin.GetValueByTagName("stid");	// 文章所在栏目ID
            string strFilterCurrentArticle = objPlugin.GetValueByTagName("filtercurrent").Trim();//过滤当前文章
            bool bolFilterCurrentArticle = false;
            int intArtSecID = -1;
            if (strFilterCurrentArticle == "1")
            {
                bolFilterCurrentArticle = true;
            }
            if (strArtSecID == string.Empty)	// 不是指定文章ID,采用当前ID
            {
                intArtSecID = _intDefaultASID;
            }
            else
            {
                intArtSecID = int.Parse(strArtSecID);
            }

            if (strSectionID != string.Empty)
            {
                _intDefaultParentID = int.Parse(strSectionID);
            }
            else
            {
                using (ArtSecRelationFactory objArtSecRelationFactory = new ArtSecRelationFactory())
                {
                    ArtSecRelation objArtSecRelation = objArtSecRelationFactory.Creator(intArtSecID);
                    if (objArtSecRelation != null)
                    {
                        _intDefaultParentID = objArtSecRelation.DataItem.intSectionID;
                    }
                    else
                    {
                        throw new Exception("无法获取 标识(intSecArtID)为" + intArtSecID + "的所属栏目");
                    }
                }
            }

            strXml = ReplaceSPlugin.ReplaceArticleListPlugin(objPlugin, Convert.ToInt32(_intDefaultParentID), sessionUser, bolFilterCurrentArticle,intArtSecID);
            // 将Xml缓存
            //XmlCach objXmlCach = new XmlCach(intArticleID, Model.Common.Plugin.PluginType.ArticleFields, 0, strXml);
            //m_aryXmlCach.Add(objXmlCach);

            return strXml;
        }

        /// <summary>
        /// 文章所在栏目的兄弟栏目列表
        /// </summary>
        /// <param name="objPlugin"></param>
        /// <returns></returns>
        public static string ReplaceArticleBrotherSectionPlugin(Plugin objPlugin, int _intDefaultASID, SessionUser objSessionUser, bool bolIsRefer, int intCurrentSectionID)
        {
            int _intDefaultParentID = -1;
            string strXml = string.Empty;
            string strArtSecID = objPlugin.GetValueByTagName("id");	// 文章ID
            string strSectionID = objPlugin.GetValueByTagName("stid");	// 文章所在栏目ID
            int intArtSecID = -1;
            if (strArtSecID == string.Empty)	// 不是指定文章ID,采用当前ID
            {
                intArtSecID = _intDefaultASID;
            }
            else
            {
                intArtSecID = int.Parse(strArtSecID);
            }

            if (bolIsRefer)
            {
                _intDefaultParentID = intCurrentSectionID;
            }

            if (strSectionID != string.Empty)
            {
                _intDefaultParentID = int.Parse(strSectionID);
            }
            else
            {
                using (ArtSecRelationFactory objArtSecRelationFactory = new ArtSecRelationFactory())
                {
                    ArtSecRelation objArtSecRelation = objArtSecRelationFactory.Creator(intArtSecID);
                    if (objArtSecRelation != null)
                    {
                        if (!bolIsRefer)
                        {
                            _intDefaultParentID = objArtSecRelation.DataItem.intSectionID; ;
                        }
                        else
                        {
                            _intDefaultParentID = intCurrentSectionID;
                            if (_intDefaultParentID <= 0)
                            {
                                throw new Exception(string.Format("栏目ID为{0}的付栏目不存在", strSectionID));
                            }
                        }
                    }
                    else
                    {
                        throw new Exception("无法获取 标识(intSecArtID)为" + intArtSecID + "的所属栏目");
                    }
                }
            }

            strXml = ReplaceSPlugin.ReplaceBrotherSectionList(objPlugin, _intDefaultParentID, objSessionUser);
            // 将Xml缓存
            //XmlCach objXmlCach = new XmlCach(intArticleID, Model.Common.Plugin.PluginType.ArticleFields, 0, strXml);
            //m_aryXmlCach.Add(objXmlCach);

            return strXml;
        }

        /// <summary>
        /// 获取文章所在栏目的父栏目信息
        /// </summary>
        /// <returns></returns>
        public static string ReplaceCurrentArticleParentSection(Plugin objPlugin, int _intDefaultASID, SessionUser objSessionUser)
        {
            int _intDefaultParentID = -1;
            string strXml = string.Empty;
            string strArtSecID = objPlugin.GetValueByTagName("id");	// 文章ID
            string strSectionID = objPlugin.GetValueByTagName("stid");	// 文章所在栏目ID
            int intArtSecID = -1;
            if (strArtSecID == string.Empty)	// 不是指定文章ID,采用当前ID
            {
                intArtSecID = _intDefaultASID;
            }
            else
            {
                intArtSecID = int.Parse(strArtSecID);
            }

            if (strSectionID != string.Empty)
            {
                _intDefaultParentID = int.Parse(strSectionID);
            }
            else
            {
                using (ArtSecRelationFactory objArtSecRelationFactory = new ArtSecRelationFactory())
                {
                    try
                    {
                        ArtSecRelation objArtSecRelation = objArtSecRelationFactory.Creator(intArtSecID);
                        if (objArtSecRelation != null)
                        {
                            //_intDefaultParentID = objArtSecRelation.DataItem.intSectionID;
                            using (SectionsFactory objSectionsFactory = new SectionsFactory())
                            {
                                try
                                {
                                    Sections objSections = objSectionsFactory.CreatorBySql(string.Format(" intSectionID in (select intParentID from Sections where intSectionID={0})", objArtSecRelation.DataItem.intSectionID));
                                    if (objSections != null)
                                    {
                                        _intDefaultParentID = objSections.DataItem.intSectionID;
                                    }
                                    else
                                    {
                                        throw new Exception("无法获取 标识(intSecArtID)为" + intArtSecID + "所属栏目的父级栏目");
                                    }
                                }
                                catch (Exception ex)
                                {
                                    throw ex;
                                }
                                finally
                                {
                                    objSectionsFactory.Dispose();
                                }
                            }
                        }
                        else
                        {
                            throw new Exception("无法获取 标识(intSecArtID)为" + intArtSecID + "的所属栏目");
                        }
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                    finally
                    {
                        objArtSecRelationFactory.Dispose();
                    }
                }
            }

            strXml = ReplaceSPlugin.ReplaceSectionPlugin(objPlugin, _intDefaultParentID, objSessionUser);
            // 将Xml缓存
            //XmlCach objXmlCach = new XmlCach(intArticleID, Model.Common.Plugin.PluginType.ArticleFields, 0, strXml);
            //m_aryXmlCach.Add(objXmlCach);

            return strXml;
        }
    }
}
